package com.clothing.service.impl;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.clothing.dao.UserDao;
import com.clothing.entity.User;
import com.clothing.service.UserService;

/**
 * @ClassName: UserServiceImpl
 * @Description: TODO(Service - 用户)
 * @author Charles.Tao
 * @date 2016年3月24日 下午4:04:27
 * @version 1.0
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User, Long> implements
        UserService
{
    @Autowired
    UserDao userDao;

    @Override
    public User findByMobile(String mobile)
    {
        return userDao.findByMobile(mobile);
    }

    @Override
    public User findByOpenid(String openid)
    {
        return userDao.findByOpenid(openid);
    }
    
    @Override
    public User getCurrent()
    {
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        
        if (requestAttributes != null)
        {
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            Object object =  request.getAttribute("user");
            
            if (null != object)
            {
                return (User) object;
            }
        }
        
        return null;
    }

    @Override
    public Long getCurrentId()
    {
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        
        if (requestAttributes != null)
        {
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            Object object =  request.getAttribute("user");
            
            if (null != object)
            {
                User user = (User) object;
                
                if (null != user)
                {
                    return user.getId();
                }
            }
        }
        
        return null;
    }
}
